#include <QPainter>
#include <QPen>
#include <QBrush>


#include "scrollBarSpec.h"
#include "globalValue.h"

scrollBarSpec::scrollBarSpec()
{
    this->setFixedWidth(20);
    this->setMinimumHeight(100);
    this->setMouseTracking(true);

    isSliderPointed = 0;
    isSliderPressed = 0;
}

void scrollBarSpec::mouseMoveEvent(QMouseEvent *event)
{
    double pos = this->value()/(double)(this->maximum()-this->minimum())
            * (this->height()-50) + 25;

    int currentValue = (int)((event->y()-25)/(double)(this->height()-50)
            * (this->maximum() - this->minimum()));
    if(event->y() > pos-25 && event->y() < pos+25)
    {
        this->setCursor(Qt::PointingHandCursor);
        isSliderPointed = 2;
    }
    else
    {
        this->setCursor(Qt::ArrowCursor);
        isSliderPointed = 0;
    }

    if(isSliderPressed == 1)
    {
        this->setValue(currentValue);
    }

    update();
}

void scrollBarSpec::mousePressEvent(QMouseEvent *event)
{
    double pos = this->value()/(double)(this->maximum()-this->minimum())
            * (this->height()-50) + 25;
    int currentValue = (int)((event->y()-25)/(double)(this->height()-50)
            * (this->maximum() - this->minimum()));
    if((event->y() >= 25 || event->y() <= this->height()-25)
            && (event->y() < pos-25 || event->y() > pos+25))
    {
        this->setValue(currentValue);
    }
    else if(event->y() >= pos-25 && event->y() <= pos+25)
    {
        isSliderPressed = 1;
    }

    update();
}

void scrollBarSpec::mouseReleaseEvent(QMouseEvent *event)
{
    isSliderPressed = 0;
}

void scrollBarSpec::paintEvent(QPaintEvent *event)
{
    double pos = this->value()/(double)(this->maximum()-this->minimum())
            * (this->height()-50) + 25;

    QColor color;

    QPainter  painter(this);

    painter.setRenderHint(QPainter::Antialiasing, true);


    color.setRgb(0x22, 0x22, 0x22, 0x22);
    painter.fillRect(this->rect(), widgetBack);

    QPen pen;
    QBrush brush;

    if(isSliderPointed == 0)
        //color.setRgb(0x11, 0x11, 0x11, 0x22);
        color.setRgb(0x22, 0x22, 0xFF, 0x77);
    else
        color.setRgb(0x22, 0x22, 0xFF, 0xCC);
    pen.setWidth(0);
    pen.setColor(color);
    painter.setPen(pen);

    brush.setColor(color);
    brush.setStyle(Qt::SolidPattern);
    painter.setBrush(brush);

    painter.drawRoundedRect(QRectF(0, pos-25, 15, 50), 5, 5);

    //painter.drawRect(QRectF(0, 0, 30, this->width()));

}


void scrollBarSpec::wheelEvent(QWheelEvent *event)
{}

void scrollBarSpec::enterEvent(QEvent *event)
{

}

void scrollBarSpec::leaveEvent(QEvent *event)
{
    isSliderPointed = 0;
    update();
}
